<?php

class My_Image_lib extends CI_Image_lib{
	function resizeThenCrop()
	{
		$orginal_aspect = $this->orig_width/$this->orig_height;

		$required_aspect = $this->width/$this->height;

		if($orginal_aspect >= $required_aspect){
			$temp_width = $this->height * $orginal_aspect;
			$temp_height = $this->height;

			// Position the image
			$xPos = floor(($this->width - $temp_width) / 2);
			$yPos = 0;
		} else {
			$temp_width = $this->width;
			$temp_height = $this->width / $orginal_aspect;

			// Position the image
			$xPos = 0;
			$yPos = floor(($this->height - $temp_height) / 2);
		}

		// If resizing the x/y axis must be zero
		$this->x_axis = 0;
		$this->y_axis = 0;

		// Create the image handle
		if ( ! ($src_img = $this->image_create_gd()))
		{
			return FALSE;
		}

		// Create The Image
		if ($this->image_library == 'gd2' AND function_exists('imagecreatetruecolor'))
		{
			$create    = 'imagecreatetruecolor';
			$copy    = 'imagecopyresampled';
		}
		else
		{
			$create    = 'imagecreate';
			$copy    = 'imagecopyresized';
		}


		// Create
		$dst_img = $create($this->width, $this->height);

		// White...
		imagefill ( $dst_img, 0, 0, imagecolorallocate ( imagecreatetruecolor ( 140, 140 ), 255, 255, 255 ) );

		// Move into workspace
		$copy($dst_img, $src_img, $xPos, $yPos, $this->x_axis, $this->y_axis, $temp_width, $temp_height, $this->orig_width, $this->orig_height);

		// Show the image
		if ($this->dynamic_output == TRUE)
		{
			$this->image_display_gd($dst_img);
		}
		else
		{
			// Or save it
			if ( ! $this->image_save_gd($dst_img))
			{
				return FALSE;
			}
		}

		// Kill the file handles
		imagedestroy($dst_img);
		imagedestroy($src_img);

		// Set the file to 777
		@chmod($this->full_dst_path, 0777);

		return TRUE;
	}
}